package com.hzit.pay.center.controller;

import com.hzit.pay.center.exception.BizException;
import com.pay.center.common.req.PayCallBackData;
import com.pay.center.common.req.PaymentQueryReq;
import com.pay.center.common.req.PaymentReq;
import com.hzit.pay.center.service.IPayService;
import com.pay.center.common.req.QueryPayMethodReq;
import com.pay.center.common.resp.PayMethodData;
import com.pay.center.common.resp.RespPayData;
import com.pay.center.common.resp.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 支付统一接口
 */

@RestController
@RequestMapping("/payment")
public class PayController {

    @Autowired
    private IPayService payService;

    private Logger logger = LoggerFactory.getLogger(PayController.class);


    /**
     * 根据商户id查询，支付列表
     */
    @RequestMapping(value = "/queryPayList",method = RequestMethod.POST)
    public Response<List<PayMethodData>> queryPayMethod(@RequestBody  @Validated QueryPayMethodReq queryPayMethodReq){
        logger.info("#########收到根据用户id查询，支付列表的请求,参数：{}##########。",queryPayMethodReq);
        Response<List<PayMethodData>> response = new Response<List<PayMethodData>>();
        List<PayMethodData> dataList = null;
        try {
            dataList = payService.queryPayMethodList(queryPayMethodReq);
            response.setCode(0);
            response.setMsg("查询成功");
            response.setData(dataList);
        } catch (BizException e) {
            logger.error("BizException",e);
            response.setCode(-1);
            response.setMsg(e.getMessage());
            return response;
        }

        logger.info("#########完成根据用户id查询支付列表的请求,返回参数：{}##########。",response);
        return response;


    }


    /**
     * 扫码支付接口
     * @param paymentReq
     */
    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    public Response<RespPayData> pay(@RequestBody @Validated PaymentReq paymentReq){
        logger.info("#########收到用户支付请求，请求参数：{}##########。",paymentReq);
        Response<RespPayData> response = new Response<RespPayData>();
        try {
            response = payService.doPay(paymentReq);
        }catch (BizException e){
            response.setCode(-1);
            response.setMsg(e.getMessage());
        }
        logger.info("#########处理文支付请求，返回参数：{}##########。",response);
        return response;

    }

    /**
     * 查询支付结果查询
     * @param paymentQueryReq
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Response<PayCallBackData> query(@RequestBody PaymentQueryReq paymentQueryReq){
        logger.info("#########收到用户支付查询请求，请求参数：{}##########。",paymentQueryReq);

        return  payService.query(paymentQueryReq);

    }


    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    public Response<List> queryList(){

        Response response = new Response();

        response.setData(payService.queryUnConfirmedList());

        return response;
    }


    /**
     * 支付关闭，防止：订单系统超时关闭了，第三方支付系统或银行的交易还没有关闭。
     * @param paymentQueryReq
     * @return
     */
    public Response<?> close(@RequestBody PaymentQueryReq paymentQueryReq){

        payService.payClose();


        return null;
    }




}
